from app.extensions import db
from sqlalchemy import Column, String, DECIMAL

class StockBalanceSheet(db.Model):
    """资产负债表数据表"""
    __tablename__ = 'stock_balance_sheet'
    
    ts_code = Column(String(20), primary_key=True, comment='TS股票代码')
    end_date = Column(String(8), primary_key=True, comment='报告期')
    ann_date = Column(String(8), comment='公告日期')
    f_ann_date = Column(String(8), comment='实际公告日期')
    report_type = Column(String(10), comment='报告类型')
    comp_type = Column(String(10), comment='公司类型')
    end_type = Column(String(10), comment='报告期类型')
    total_share = Column(DECIMAL(20, 4), comment='期末总股本')
    cap_rese = Column(DECIMAL(20, 4), comment='资本公积金')
    undistr_porfit = Column(DECIMAL(20, 4), comment='未分配利润')
    surplus_rese = Column(DECIMAL(20, 4), comment='盈余公积金')
    special_rese = Column(DECIMAL(20, 4), comment='专项储备')
    money_cap = Column(DECIMAL(20, 4), comment='货币资金')
    trad_asset = Column(DECIMAL(20, 4), comment='交易性金融资产')
    notes_receiv = Column(DECIMAL(20, 4), comment='应收票据')
    accounts_receiv = Column(DECIMAL(20, 4), comment='应收账款')
    oth_receiv = Column(DECIMAL(20, 4), comment='其他应收款')
    prepayment = Column(DECIMAL(20, 4), comment='预付款项')
    div_receiv = Column(DECIMAL(20, 4), comment='应收股利')
    int_receiv = Column(DECIMAL(20, 4), comment='应收利息')
    inventories = Column(DECIMAL(20, 4), comment='存货')
    amor_exp = Column(DECIMAL(20, 4), comment='长期待摊费用')
    nca_within_1y = Column(DECIMAL(20, 4), comment='一年内到期的非流动资产')
    sett_rsrv = Column(DECIMAL(20, 4), comment='结算备付金')
    loanto_oth_bank_fi = Column(DECIMAL(20, 4), comment='拆出资金')
    premium_receiv = Column(DECIMAL(20, 4), comment='应收保费')
    reinsur_receiv = Column(DECIMAL(20, 4), comment='应收分保账款')
    reinsur_res_receiv = Column(DECIMAL(20, 4), comment='应收分保合同准备金')
    pur_resale_fa = Column(DECIMAL(20, 4), comment='买入返售金融资产')
    oth_cur_assets = Column(DECIMAL(20, 4), comment='其他流动资产')
    total_cur_assets = Column(DECIMAL(20, 4), comment='流动资产合计')
    fa_avail_for_sale = Column(DECIMAL(20, 4), comment='可供出售金融资产')
    htm_invest = Column(DECIMAL(20, 4), comment='持有至到期投资')
    lt_eqt_invest = Column(DECIMAL(20, 4), comment='长期股权投资')
    invest_real_estate = Column(DECIMAL(20, 4), comment='投资性房地产')
    time_deposits = Column(DECIMAL(20, 4), comment='定期存款')
    oth_assets = Column(DECIMAL(20, 4), comment='其他资产')
    lt_rec = Column(DECIMAL(20, 4), comment='长期应收款')
    fix_assets = Column(DECIMAL(20, 4), comment='固定资产')
    cip = Column(DECIMAL(20, 4), comment='在建工程')
    const_materials = Column(DECIMAL(20, 4), comment='工程物资')
    fixed_assets_disp = Column(DECIMAL(20, 4), comment='固定资产清理')
    produc_bio_assets = Column(DECIMAL(20, 4), comment='生产性生物资产')
    oil_and_gas_assets = Column(DECIMAL(20, 4), comment='油气资产')
    intan_assets = Column(DECIMAL(20, 4), comment='无形资产')
    r_and_d = Column(DECIMAL(20, 4), comment='开发支出')
    goodwill = Column(DECIMAL(20, 4), comment='商誉')
    lt_amor_exp = Column(DECIMAL(20, 4), comment='长期待摊费用')
    defer_tax_assets = Column(DECIMAL(20, 4), comment='递延所得税资产')
    decr_in_disbur = Column(DECIMAL(20, 4), comment='发放贷款及垫款')
    oth_nca = Column(DECIMAL(20, 4), comment='其他非流动资产')
    total_nca = Column(DECIMAL(20, 4), comment='非流动资产合计')
    cash_reser_cb = Column(DECIMAL(20, 4), comment='现金及存放中央银行款项')
    depos_in_oth_bfi = Column(DECIMAL(20, 4), comment='存放同业和其它金融机构款项')
    prec_metals = Column(DECIMAL(20, 4), comment='贵金属')
    deriv_assets = Column(DECIMAL(20, 4), comment='衍生金融资产')
    rr_reins_une_prem = Column(DECIMAL(20, 4), comment='应收分保未到期责任准备金')
    rr_reins_outstd_cla = Column(DECIMAL(20, 4), comment='应收分保未决赔款准备金')
    rr_reins_lins_liab = Column(DECIMAL(20, 4), comment='应收分保寿险责任准备金')
    rr_reins_lthins_liab = Column(DECIMAL(20, 4), comment='应收分保长期健康险责任准备金')
    refund_depos = Column(DECIMAL(20, 4), comment='存出保证金')
    ph_pledge_loans = Column(DECIMAL(20, 4), comment='保户质押贷款')
    refund_cap_depos = Column(DECIMAL(20, 4), comment='存出资本保证金')
    indep_acct_assets = Column(DECIMAL(20, 4), comment='独立账户资产')
    client_depos = Column(DECIMAL(20, 4), comment='其中：客户资金存款')
    client_prov = Column(DECIMAL(20, 4), comment='其中：客户备付金')
    transac_seat_fee = Column(DECIMAL(20, 4), comment='其中:交易席位费')
    invest_as_receiv = Column(DECIMAL(20, 4), comment='应收款项类投资')
    total_assets = Column(DECIMAL(20, 4), comment='资产总计')
    lt_borr = Column(DECIMAL(20, 4), comment='长期借款')
    st_borr = Column(DECIMAL(20, 4), comment='短期借款')
    cb_borr = Column(DECIMAL(20, 4), comment='向中央银行借款')
    depos_ib_deposits = Column(DECIMAL(20, 4), comment='吸收存款及同业存放')
    loan_oth_bank = Column(DECIMAL(20, 4), comment='拆入资金')
    trading_fl = Column(DECIMAL(20, 4), comment='交易性金融负债')
    notes_payable = Column(DECIMAL(20, 4), comment='应付票据')
    acct_payable = Column(DECIMAL(20, 4), comment='应付账款')
    adv_receipts = Column(DECIMAL(20, 4), comment='预收款项')
    sold_for_repur_fa = Column(DECIMAL(20, 4), comment='卖出回购金融资产款')
    comm_payable = Column(DECIMAL(20, 4), comment='应付手续费及佣金')
    payroll_payable = Column(DECIMAL(20, 4), comment='应付职工薪酬')
    taxes_payable = Column(DECIMAL(20, 4), comment='应交税费')
    int_payable = Column(DECIMAL(20, 4), comment='应付利息')
    div_payable = Column(DECIMAL(20, 4), comment='应付股利')
    oth_payable = Column(DECIMAL(20, 4), comment='其他应付款')
    acc_exp = Column(DECIMAL(20, 4), comment='预提费用')
    deferred_inc = Column(DECIMAL(20, 4), comment='递延收益')
    st_bonds_payable = Column(DECIMAL(20, 4), comment='应付短期债券')
    payable_to_reinsurer = Column(DECIMAL(20, 4), comment='应付分保账款')
    rsrv_insur_cont = Column(DECIMAL(20, 4), comment='保险合同准备金')
    acting_trading_sec = Column(DECIMAL(20, 4), comment='代理买卖证券款')
    acting_uw_sec = Column(DECIMAL(20, 4), comment='代理承销证券款')
    non_cur_liab_due_1y = Column(DECIMAL(20, 4), comment='一年内到期的非流动负债')
    oth_cur_liab = Column(DECIMAL(20, 4), comment='其他流动负债')
    total_cur_liab = Column(DECIMAL(20, 4), comment='流动负债合计')
    bond_payable = Column(DECIMAL(20, 4), comment='应付债券')
    lt_payable = Column(DECIMAL(20, 4), comment='长期应付款')
    specific_payables = Column(DECIMAL(20, 4), comment='专项应付款')
    estimated_liab = Column(DECIMAL(20, 4), comment='预计负债')
    defer_tax_liab = Column(DECIMAL(20, 4), comment='递延所得税负债')
    defer_inc_non_cur_liab = Column(DECIMAL(20, 4), comment='递延收益-非流动负债')
    oth_ncl = Column(DECIMAL(20, 4), comment='其他非流动负债')
    total_ncl = Column(DECIMAL(20, 4), comment='非流动负债合计')
    depos_oth_bfi = Column(DECIMAL(20, 4), comment='同业和其它金融机构存放款项')
    deriv_liab = Column(DECIMAL(20, 4), comment='衍生金融负债')
    depos = Column(DECIMAL(20, 4), comment='吸收存款')
    agency_bus_liab = Column(DECIMAL(20, 4), comment='代理业务负债')
    oth_liab = Column(DECIMAL(20, 4), comment='其他负债')
    prem_receiv_adva = Column(DECIMAL(20, 4), comment='预收保费')
    depos_received = Column(DECIMAL(20, 4), comment='存入保证金')
    ph_invest = Column(DECIMAL(20, 4), comment='保户储金及投资款')
    reser_une_prem = Column(DECIMAL(20, 4), comment='未到期责任准备金')
    reser_outstd_claims = Column(DECIMAL(20, 4), comment='未决赔款准备金')
    reser_lins_liab = Column(DECIMAL(20, 4), comment='寿险责任准备金')
    reser_lthins_liab = Column(DECIMAL(20, 4), comment='长期健康险责任准备金')
    indept_acc_liab = Column(DECIMAL(20, 4), comment='独立账户负债')
    pledge_borr = Column(DECIMAL(20, 4), comment='其中：质押借款')
    indem_payable = Column(DECIMAL(20, 4), comment='应付赔付款')
    policy_div_payable = Column(DECIMAL(20, 4), comment='应付保单红利')
    total_liab = Column(DECIMAL(20, 4), comment='负债合计')
    treasury_share = Column(DECIMAL(20, 4), comment='减:库存股')
    ordin_risk_reser = Column(DECIMAL(20, 4), comment='一般风险准备')
    forex_differ = Column(DECIMAL(20, 4), comment='外币报表折算差额')
    invest_loss_unconf = Column(DECIMAL(20, 4), comment='未确认投资损失')
    minority_int = Column(DECIMAL(20, 4), comment='少数股东权益')
    total_hldr_eqy_exc_min_int = Column(DECIMAL(20, 4), comment='股东权益合计(不含少数股东权益)')
    total_hldr_eqy_inc_min_int = Column(DECIMAL(20, 4), comment='股东权益合计(含少数股东权益)')
    total_liab_hldr_eqy = Column(DECIMAL(20, 4), comment='负债及股东权益总计')
    lt_payroll_payable = Column(DECIMAL(20, 4), comment='长期应付职工薪酬')
    oth_comp_income = Column(DECIMAL(20, 4), comment='其他综合收益')
    oth_eqt_tools = Column(DECIMAL(20, 4), comment='其他权益工具')
    oth_eqt_tools_p_shr = Column(DECIMAL(20, 4), comment='其他权益工具(优先股)')
    lending_funds = Column(DECIMAL(20, 4), comment='融出资金')
    acc_receivable = Column(DECIMAL(20, 4), comment='应收款项')
    st_fin_payable = Column(DECIMAL(20, 4), comment='应付短期融资款')
    payables = Column(DECIMAL(20, 4), comment='应付款项')
    hfs_assets = Column(DECIMAL(20, 4), comment='持有待售资产')
    hfs_sales = Column(DECIMAL(20, 4), comment='持有待售负债')
    cost_fin_assets = Column(DECIMAL(20, 4), comment='以摊余成本计量的金融资产')
    fair_value_fin_assets = Column(DECIMAL(20, 4), comment='以公允价值计量且其变动计入其他综合收益的金融资产')
    contract_assets = Column(DECIMAL(20, 4), comment='合同资产')
    contract_liab = Column(DECIMAL(20, 4), comment='合同负债')
    accounts_receiv_bill = Column(DECIMAL(20, 4), comment='应收票据及应收账款')
    accounts_pay = Column(DECIMAL(20, 4), comment='应付票据及应付账款')
    oth_rcv_total = Column(DECIMAL(20, 4), comment='其他应收款(合计)')
    fix_assets_total = Column(DECIMAL(20, 4), comment='固定资产(合计)')
    cip_total = Column(DECIMAL(20, 4), comment='在建工程(合计)')
    oth_pay_total = Column(DECIMAL(20, 4), comment='其他应付款(合计)')
    long_pay_total = Column(DECIMAL(20, 4), comment='长期应付款(合计)')
    debt_invest = Column(DECIMAL(20, 4), comment='债权投资')
    oth_debt_invest = Column(DECIMAL(20, 4), comment='其他债权投资')
    update_flag = Column(String(1), comment='更新标识')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'ts_code': self.ts_code,
            'end_date': self.end_date,
            'ann_date': self.ann_date,
            'f_ann_date': self.f_ann_date,
            'report_type': self.report_type,
            'comp_type': self.comp_type,
            'end_type': self.end_type,
            'total_assets': float(self.total_assets) if self.total_assets else None,
            'total_hldr_eqy_exc_min_int': float(self.total_hldr_eqy_exc_min_int) if self.total_hldr_eqy_exc_min_int else None,
            'total_liab': float(self.total_liab) if self.total_liab else None,
            'money_cap': float(self.money_cap) if self.money_cap else None,
        }
    
    def __repr__(self):
        return f'<StockBalanceSheet {self.ts_code} {self.end_date}>' 